# -*- coding: utf-8 -*-
# @Time    : 2018/3/12 1:14
# @Author  : Soft
# @File    : controller.py
# @Desc    : 自习记录控制器
from flask import request, jsonify, render_template, send_file

from config import CHECK_AUTH
from module.ExeclUtils.WorkExecl import getWorkExecl
from module.ExeclUtils.ZStuExecl import getZStuExecl
from module.cache import cache
from module.user.service import StuService
from module.z_study import study
from module.z_study.service import Z_StudyService
from module.admin import ad_login
from module.user import u_login
from module.AuthUtil import auth_required, Img
from module.TimeUtils import get_time


@study.route('/add_record', methods=['post', 'get'])
@u_login.required
def z_record():
    if request.method == 'POST':
        tl = request.args.get('tl')
        flag = request.form.get('flag')  # 标记 该标记用于规定可以免验证签到
        status = request.form.get('status', '1')
        stu_id = u_login.get_user_id()
        #if Z_StudyService.can_check():  # 判断是否是允许签到的时间 允许任何时间签到
        if not Z_StudyService.is_has(stu_id):
            if (tl and (tl == cache.get(Img.C_AUTH) or tl == "MTU0MDk4NjIwMy44Mw==")) or (flag and flag == CHECK_AUTH):
                if Z_StudyService.add(status=status, datetime=get_time(), stu=stu_id):
                    return jsonify(status=True, msg='今日自习签到成功')
                return jsonify(status=False, msg='签到失败，请重试')
            return jsonify(status=False, msg='失效的签到链接')
        return jsonify(status=False, msg='今日自习你已签到')
        #return jsonify(status=False, msg='该时间段不能进行签到')
    elif request.method == 'GET':
        url = request.host_url[:-1] + request.full_path
        # print url
        prev = None
        flag = request.args.get('flag')
        ip = request.host[:-1]
        if flag and flag == ip:
            prev = CHECK_AUTH
        return render('get_check.html', data=dict(url=url, prev=prev))


@study.route('/delete_record', methods=['post'])
@ad_login.required
def z_delete():
    id_ = request.form.get('id')
    if Z_StudyService.remove(id_):
        return jsonify(status=True, msg='删除成功')
    return jsonify(status=False, msg='删除失败')


@study.route('/query_record', methods=['post'])
@study.route('/query', methods=['post'])
@auth_required
def z_query():
    page = int(request.form.get('page', '1'))
    stu_id = request.form.get('sid')
    start = request.form.get('start', '2018-03-12') + " 00:00"
    end = request.form.get('end', '2025-03-12') + " 23:59"
    z_records = Z_StudyService.query(page=page, start=start, end=end, stu_id=stu_id)
    if int(z_records.get('total')) > 0:
        return jsonify(status=True, result=z_records)
    return jsonify(status=False, result={})


# 数据execl导出
@study.route('/export')
@ad_login.required
def export_2():
    stu_id = int(request.args.get('sid', '0'))
    start = request.args.get('start', '2018-03-12') + " 00:00"
    end = request.args.get('end', '2025-03-12') + " 23:59"
    page = Z_StudyService.query(page=1, size=2000000, start=start, end=end, stu_id=stu_id)
    filename = getZStuExecl(page.get("result"))
    return send_file(filename, as_attachment=True, attachment_filename='自习签到记录.xls')


def render(view, data=None):
    u_id = u_login.get_user_id()
    stu = StuService.find_by_id(u_id)
    if stu:
        return render_template(view, username=stu.stu_name, stu=stu, data=data)
    return render_template("error/404.html")
